package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperUtil {
    private static SqlSessionFactory factory = null;
    private MapperUtil() {
    }

    static {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("sql.xml");
            factory = new SqlSessionFactoryBuilder().build(is);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取session
    public static SqlSession getSession() {
        return factory.openSession();
    }
    //释放资源
    public static void close(SqlSession session) {
        session.commit();
        session.close();
    }
}
